package com.csmz.mybookreadernew.UI.Activity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Handler;
import android.support.annotation.NonNull;
import android.support.constraint.ConstraintLayout;
import android.support.design.widget.BottomNavigationView;
import android.support.design.widget.NavigationView;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.GravityCompat;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatDelegate;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.SPUtils;
import com.blankj.utilcode.util.Utils;
import com.csmz.mybookreadernew.Adapter.FragmentAdapter;
import com.csmz.mybookreadernew.R;
import com.csmz.mybookreadernew.UI.Fragment.MyFragment;
import com.csmz.mybookreadernew.UI.Fragment.MyReadFragment;
import com.csmz.mybookreadernew.UI.Fragment.MyVideoFragment;
import com.csmz.mybookreadernew.UI.View.MyInfoView;
import com.king.base.BaseActivity;
import com.king.base.util.ToastUtils;

import java.util.ArrayList;
import java.util.List;


import butterknife.BindView;
import butterknife.ButterKnife;

public class MainActivity extends BaseActivity
        implements NavigationView.OnNavigationItemSelectedListener {

    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.navigation)
    BottomNavigationView navigation;
    @BindView(R.id.container)
    ConstraintLayout container;
    @BindView(R.id.nav_view)
    NavigationView navView;
    @BindView(R.id.drawer_layout)
    DrawerLayout drawerLayout;
    @BindView(R.id.main_viewpager)
    ViewPager viewPager;
    @BindView(R.id.tab_layout)
    TabLayout tabLayout;
    private MyInfoView mMyInfoView;
    private ActionBarDrawerToggle toggle;
    private List<Fragment> fragments;
    private MyReadFragment myReadFragment;
    private MyVideoFragment myVideoFragment;
    private MyFragment myFragment;
    private FragmentAdapter adapter;
    private MenuItem menuItem;
    private View view;

    @Override
    public void initUI() {
        setContentView(R.layout.activity_main);
        Utils.init(this);
        ButterKnife.bind(this);
        initCen();
    }

    private void initCen() {
        setSupportActionBar(toolbar);
        fragments = new ArrayList<>();
        myReadFragment =  new MyReadFragment();
        myVideoFragment = new MyVideoFragment();
        myFragment = new MyFragment();
        fragments.add(myReadFragment);
        fragments.add(myVideoFragment);
        fragments.add(myFragment);
        adapter = new FragmentAdapter(getSupportFragmentManager(),fragments);
        viewPager.setAdapter(adapter);
        viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int i, float v, int i1) {

            }

            @Override
            public void onPageSelected(int i) {
                if (menuItem != null){
                    menuItem.setChecked(false);
                }else {
                    navigation.getMenu().getItem(0).setChecked(false);
                }
                menuItem = navigation.getMenu().getItem(i);
                menuItem.setChecked(true);
            }

            @Override
            public void onPageScrollStateChanged(int i) {

            }
        });
        toggle = new ActionBarDrawerToggle(
                this, drawerLayout, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawerLayout.addDrawerListener(toggle);
        toggle.syncState();
        navView.setNavigationItemSelectedListener(this);
        navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
        tabLayout.setVisibility(View.GONE);
        LayoutInflater inflater=getLayoutInflater();
        view=inflater.inflate(R.layout.nav_header_main,null);
        TextView textView=view.findViewById(R.id.tv_name);
        String userName=SPUtils.getInstance("QQ_User_Name").getString("user_name","用户名");
        textView.setText(userName);
        textView.setVisibility(View.GONE);
        LogUtils.d(SPUtils.getInstance("user").getString("iPhone"));
    }

    @Override
    public void initData() {

    }

    @Override
    public void addListeners() {

    }

    @Override
    public void onBackPressed() {
        if (drawerLayout.isDrawerOpen(GravityCompat.START)) {
            drawerLayout.closeDrawer(GravityCompat.START);
        } else {
            super.onBackPressed();
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        //noinspection SimplifiableIfStatement
        if (id == R.id.action_item1) {
            if (readLoginStatus()){
                startActivity(new Intent(MainActivity.this,LoginActivity.class));
            }else {
                ToastUtils.showToast(MainActivity.this,"已登陆!");
            }
            return true;
        } else if (id == R.id.action_item2) {
            ToastUtils.showToast(MainActivity.this,"刷新失败!");
            return true;
        }else if (id == R.id.action_item3) {
            ToastUtils.showToast(MainActivity.this,"切换失败!");
            return true;
        }


        return super.onOptionsItemSelected(item);
    }
    //从SharedPreferences中读取登录用户名
    private boolean readLoginStatus() {
        SharedPreferences sp = getSharedPreferences("LoginInfo", Context.MODE_PRIVATE);
        boolean isLogin=sp.getBoolean("isLogin",false);
        return isLogin;
    }

    @SuppressWarnings("StatementWithEmptyBody")
    @Override
    public boolean onNavigationItemSelected(MenuItem item) {
        // Handle navigation view item clicks here.
        int id = item.getItemId();

        if (id == R.id.nav_item1) {
            ToastUtils.showToast(MainActivity.this,"无消息!");
        } else if (id == R.id.nav_item2) {
            ToastUtils.showToast(MainActivity.this,"获取失败!");
        } else if (id == R.id.nav_item3) {
            boolean isDayOrNight = SPUtils.getInstance("day").getBoolean("isNight",false);
            if (isDayOrNight){
                getDelegate().setLocalNightMode(AppCompatDelegate.MODE_NIGHT_NO);
                SPUtils.getInstance("day").put("isNight",false);
            }else {
                getDelegate().setLocalNightMode(AppCompatDelegate.MODE_NIGHT_YES);
                SPUtils.getInstance("day").put("isNight",true);
            }
        }
        drawerLayout.closeDrawer(GravityCompat.START);
        return true;
    }
    private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
            = new BottomNavigationView.OnNavigationItemSelectedListener() {
        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {
            int id = item.getItemId();
            if (id == R.id.navigation_item1) {
                    viewPager.setCurrentItem(0);
                return true;
            } else if (id == R.id.navigation_item2) {
                    viewPager.setCurrentItem(1);
                return true;
            } else if (id == R.id.navigation_item3) {
                    viewPager.setCurrentItem(2);
                return true;
            }
            return false;
        }
    };

    protected void onActivityResult(int requestCode, int resultCode, Intent data){
        super.onActivityResult(requestCode,resultCode,data);
        if (data!=null){
            //从设置界面或登录界面传递过来的登录状态
            boolean isLogin=data.getBooleanExtra("isLogin",false);
            if (mMyInfoView != null){
                //登录成功或退出登录时根据isLogin设置我的界面
                mMyInfoView.setLoginParams(isLogin);
            }

        }

    }
}
